package com.studentManager;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentManage {
    public static void main(String[] args) {
        ArrayList<Student> arrayList = new ArrayList<>();
        while (true) {
            //完成主界面的编写内容
            System.out.println("--------欢迎来到学生管理系统---------");
            System.out.println("1 添加学生");
            System.out.println("2 修改学生");
            System.out.println("3 删除学生");
            System.out.println("4 查看所有学生");
            System.out.println("5 添加学生");
            System.out.println("请输入您的选择:");

            //键盘录入内容信息
            Scanner scanner = new Scanner(System.in);
            String line = scanner.nextLine();

            //开始循环判断
            switch (line) {
                case "1":
                    addStudent(arrayList);
                    break;
                case "2":
//                    System.out.println("修改学生");
                    updateStudent(arrayList);
                    break;
                case "3":
//                    System.out.println("删除学生");
                    removeStudent(arrayList);
                    break;
                case "4":
//                    System.out.println("查看所有学生");
                    showStudent(arrayList);
                    break;
                case "5":
                    System.out.println("谢谢使用");
//                    break;
                    System.exit(0);
                default:
                    System.out.println("输入选择有误,重新选择");
//                    break;
//                    System.exit(0);
                    break;
            }
        }

    }

    //添加学生的基本信息
    public static void addStudent(ArrayList<Student> arrayList) {
        //控制台输入内容
        Scanner scanner = new Scanner(System.in);
        String sid;
        while (true) {
            //提示信息
            System.out.println("输入学号");
            sid = scanner.nextLine();
            //判断学号是否存在
            int index = existsStudent(arrayList, sid);
            if (index > -1) {
                System.out.println("学号已经存在，不能重复添加");
            } else {
                break;
            }
        }

        System.out.println("输入姓名");
        String name = scanner.nextLine();
        System.out.println("输入年龄");
        String age = scanner.nextLine();
        System.out.println("输入地址");
        String address = scanner.nextLine();

        //添加到学生对象中去
        Student student = new Student(sid, name, age, address);
        arrayList.add(student);
        System.out.println("添加学生成功");
    }

    //查看学生的基本方法信息展示
    public static void showStudent(ArrayList<Student> arrayList) {
        if (arrayList.size() <= 0) {
            System.out.println("暂无数据,请添加后再查看");
            return;
        }
        //开始渲染学生的基本信息
        System.out.println("学号\t\t\t姓名\t\t年龄\t地址");
        System.out.println("--------------------");
        for (int i = 0; i < arrayList.size(); i++) {
            Student s = arrayList.get(i);
            System.out.println(s.getSid() + "\t" + s.getName() + "\t" + s.getAge() + "\t\t" + s.getAddress());
        }
    }

    //删除学生的基本信息方法
    public static void removeStudent(ArrayList<Student> arrayList) {
        //输入学生基本信息
        Scanner scanner = new Scanner(System.in);
        int ind = 0;
        while (true) {
            System.out.println("请输入你要删除学生的学号:");
            String sid = scanner.nextLine();
            int index = existsStudent(arrayList, sid);
            if (index == -1) {
                ind++;
                System.out.println("学生学号不存在请重新输入");
                if(ind>=3){
                    System.out.println("超过三次自动推出");
                    break;
                }
            } else {
                arrayList.remove(index);
                System.out.println("删除成功");
                break;
            }
        }

    }

    public static void updateStudent(ArrayList<Student> arrayList) {
        //控制台输入内容
        Scanner scanner = new Scanner(System.in);
        //提示信息
        System.out.println("输入修改学生的学号");
        String sid = scanner.nextLine();
        int index = existsStudent(arrayList, sid);
        if (index == -1) {
            System.out.println("学号不存在，请重新选择输入");
            return;
        }
        System.out.println("输入新姓名");
        String name = scanner.nextLine();
        System.out.println("输入新年龄");
        String age = scanner.nextLine();
        System.out.println("输入新地址");
        String address = scanner.nextLine();

        //添加到学生对象中去
        Student student = new Student(sid, name, age, address);

        //循环找到要修改的对象信息
        arrayList.set(index, student);
        System.out.println("更新学生信息成功");
    }

    //判断学生学号是否存在,进行一个函数的封装进行操作；
    public static int existsStudent(ArrayList<Student> arrayList, String sid) {
        //定义数据的信息
        int index = -1;
        for (int i = 0; i < arrayList.size(); i++) {
            Student s = arrayList.get(i);

            if (s.getSid().equals(sid)) {
                index = i;
                break;
            }
        }
        return index;
    }
}
